import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hippo_api.settings.dev')
django.setup()


import traceback
import paramiko
from paramiko.ssh_exception import AuthenticationException

ssh = paramiko.SSHClient()
SSH_PRIVATE_KEY = '/home/hkxpz/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY, password='135135')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 指纹记录的保存，自动做的

try:
    # ssh.connect(hostname='132.232.3.213', port=9622, username='awen', password='xxxxx..', timeout=10)  # 1. 直接密码远程连接的方式
    ssh.connect(hostname='132.232.3.213', port=9622, username='awen', pkey=key, timeout=10)  # 2. 使用秘钥免密登录的方式
    stdin, stdout, stderr = ssh.exec_command('pwd')
    result = stdout.read()
    print(result)
    ssh.close()
except AuthenticationException as e:
    print(str(e))
    print('连接失败，请检查参数是否正确！')
    print(traceback.format_exc())
